//
//  ZCSAlbumListNoXibTableViewCell.m
//  聆听FM
//
//  Created by dllo on 16/6/13.
//  Copyright © 2016年 Dark. All rights reserved.
//

#import "ZCSAlbumListNoXibTableViewCell.h"

@implementation ZCSAlbumListNoXibTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.imageBackView = [[UIView alloc] init];
        self.imageBackView.backgroundColor = [UIColor colorWithWhite:0.895 alpha:1.000];
        [self addSubview:_imageBackView];
        
        self.leftImageView = [[UIImageView alloc] init];
        [self.imageBackView addSubview:_leftImageView];
        
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.numberOfLines = 0;
        self.titleLabel.font = [UIFont systemFontOfSize:13];
        [self addSubview:_titleLabel];
        
        self.timeLabel = [[UILabel alloc] init];
        self.timeLabel.font = [UIFont systemFontOfSize:12];
        self.timeLabel.textAlignment = 2;
        self.timeLabel.alpha = 0.5;
        [self addSubview:_timeLabel];
        
        self.numbelLabel = [[UILabel alloc] init];
        self.numbelLabel.font = [UIFont systemFontOfSize:12];
        self.numbelLabel.alpha = 0.5;
        [self addSubview:_numbelLabel];
        
        self.playTimeLabel = [[UILabel alloc] init];
        self.playTimeLabel.font = [UIFont systemFontOfSize:12];
        self.playTimeLabel.alpha = 0.5;
        [self addSubview:_playTimeLabel];
        
        self.playImage = [[UIImageView alloc] init];
        self.playImage.image = [UIImage imageNamed:@"sound_playtimes"];
        [self addSubview:_playImage];
        
        self.timeImage = [[UIImageView alloc] init];
        self.timeImage.image = [UIImage imageNamed:@"sound_duration"];
        [self addSubview:_timeImage];
        
        self.line = [[UIView alloc] init];
        self.line.backgroundColor = [UIColor blackColor];
        self.line.alpha = 0.2;
        [self addSubview:_line];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.imageBackView.frame = CGRectMake(7 * FITWIDTH, 7 * FITHEIGHT, 46 * FITWIDTH, 46 * FITHEIGHT);
    
    self.leftImageView.frame = CGRectMake(3 * FITWIDTH, 3 * FITHEIGHT, 40 * FITWIDTH, 40 * FITHEIGHT);
    self.imageBackView.layer.cornerRadius = 23 * FITHEIGHT;
    self.leftImageView.layer.cornerRadius = 20 * FITHEIGHT;
    self.leftImageView.layer.masksToBounds = YES;

    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:13], NSFontAttributeName, nil];
    CGRect newRect = [self.model.title boundingRectWithSize:CGSizeMake(self.frame.size.width - self.imageBackView.frame.size.width - 90 * FITWIDTH, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    self.titleLabel.frame = CGRectMake(self.imageBackView.frame.origin.x + self.imageBackView.frame.size.width + 10 * FITWIDTH, self.imageBackView.frame.origin.y, self.frame.size.width - self.imageBackView.frame.size.width - 90 * FITWIDTH, newRect.size.height * FITHEIGHT);
    self.timeLabel.frame = CGRectMake(self.frame.size.width - 60 * FITWIDTH, self.titleLabel.frame.origin.y, 50 * FITWIDTH, 12 * FITHEIGHT);
    self.playImage.frame = CGRectMake(self.imageBackView.frame.origin.x + self.imageBackView.frame.size.width + 10 * FITWIDTH , self.frame.size.height - 20 * FITHEIGHT, 10 * FITWIDTH, 10 * FITHEIGHT);
    self.numbelLabel.frame = CGRectMake(self.playImage.frame.origin.x + self.playImage.frame.size.width + 3 * FITWIDTH, self.playImage.frame.origin.y, 50 * FITWIDTH, 12 * FITHEIGHT);
    self.timeImage.frame = CGRectMake(self.numbelLabel.frame.origin.x + self.numbelLabel.frame.size.width, self.numbelLabel.frame.origin.y, 10 * FITWIDTH, 10 * FITWIDTH);
    
    self.playTimeLabel.frame = CGRectMake(self.timeImage.frame.origin.x + self.timeImage.frame.size.width + 3 * FITWIDTH, self.timeImage.frame.origin.y, 80 * FITWIDTH, 12 * FITHEIGHT);
    self.line.frame = CGRectMake(self.titleLabel.frame.origin.x, self.frame.size.height - 1, self.frame.size.width - 65 * FITWIDTH, 1);
}

- (void)setModel:(ZCSSpecialListModel *)model
{
    if (_model != model) {
        _model = model;
        [self.leftImageView sd_setImageWithURL:[NSURL URLWithString:model.coverSmall] placeholderImage:[UIImage sd_animatedGIFNamed:@"占位图"]];
        self.titleLabel.text = model.title;
        if (model.playtimes > 10000) {
            self.numbelLabel.text = [NSString stringWithFormat:@"%.1f万", model.playtimes / 10000.0];
        }
        else
        {
        self.numbelLabel.text = [NSString stringWithFormat:@"%ld", model.playtimes];
        }
        
        NSString *minString = [NSString stringWithFormat:@"%02ld", model.duration / 60];
        NSString *second = [NSString stringWithFormat:@"%02ld", model.duration % 60];
        self.playTimeLabel.text = [NSString stringWithFormat:@"%@:%@", minString, second];
        //将字符串转化为整型并转换为NSDate
        NSInteger befor = model.createdAt;
        NSDate *beforTime = [NSDate dateWithTimeIntervalSince1970:befor];
        //得到距1970的时间差
        NSTimeInterval late=[beforTime timeIntervalSince1970];
        late = late / 1000;
        //得到现在的时间
        NSDate *dat = [NSDate dateWithTimeIntervalSinceNow:0];
        //得到现在时间距1970的时间差
        NSTimeInterval now = [dat timeIntervalSince1970];
        
        //初始化时间差字符串
        NSString *timeString = @"";
        //得到发表时间距现在的时间差
        NSTimeInterval cha = now - late;
        // NSLog(@"%.f, %.f, %.f", now, late, cha);
        //小于一小时
        if (cha/3600<1) {
            timeString = [NSString stringWithFormat:@"%f", cha/60];
            timeString = [timeString substringToIndex:timeString.length-7];
            timeString=[NSString stringWithFormat:@"%@分钟前", timeString];
        }
        //小于一天
        if (cha/3600>1&&cha/86400<1) {
            timeString = [NSString stringWithFormat:@"%f", cha/3600];
            timeString =      [timeString substringToIndex:timeString.length-7];
            timeString = [NSString stringWithFormat:@"%@小时前", timeString];
        }
        //大于一天
        if (cha/86400>1)
        {
            timeString = [NSString stringWithFormat:@"%f", cha/86400];
            timeString = [timeString substringToIndex:timeString.length-7];
            timeString = [NSString stringWithFormat:@"%@天前", timeString];
        }
        //大于一年
        if (cha / 31536000) {
            NSString *str= [NSString stringWithFormat:@"%lld",model.createdAt];//时间戳
            
            NSTimeInterval time=[str doubleValue]/1000;
            
            NSDate*detaildate=[NSDate dateWithTimeIntervalSince1970:time];
            
            //实例化一个NSDateFormatter对象
            
            NSDateFormatter*dateFormatter = [[NSDateFormatter alloc]init];
            
            //设定时间格式,这里可以设置成自己需要的格式
            
            [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            
            NSString*currentDateStr = [dateFormatter stringFromDate:detaildate];
            //  NSLog(@"%@", currentDateStr);
            
            timeString = [currentDateStr substringWithRange:NSMakeRange(0, 7)];
        }
        self.timeLabel.text = timeString;
        
    }
}








- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
